﻿using EnglishAuction.BUS;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace EnglishAuction
{
    public class Temp
    {
        public int Id { get; set; }
        public int SanPhamId { get; set; }
        public string TenSanPham { get; set; }
        public double GiaSan { get; set; }
        public double GiaHienTai { get; set; }
        public DateTime ThoiGian { get; set; }
        public string HinhAnh { get; set; }
    }

    public partial class AuctionLog : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            NguoiDung nguoiDung = (NguoiDung)Session["CurrentUser"];
            if (nguoiDung == null)
            {
                Response.Redirect("Default.aspx");
                return;
            }
            else if (nguoiDung.LoaiNguoiDungId == 1)
            {
                Response.Redirect("Default.aspx");
                return;
            }

            if (!Page.IsPostBack)
            {
                var dauGias = DauGiaBUS.GetAll(nguoiDung.Id);
                List<Temp> all = new List<Temp>();
                foreach (var n in dauGias)
                {
                    Temp t = new Temp();
                    t.Id = n.Id;
                    t.SanPhamId = n.SanPhamId.Value;
                    t.TenSanPham = SanPhamBUS.GetById(n.SanPhamId.Value).TenSanPham;
                    t.GiaSan = SanPhamBUS.GetById(n.SanPhamId.Value).GiaSan.Value;
                    t.GiaHienTai = n.GiaHienTai.Value;
                    t.ThoiGian = n.ThoiGian.Value;
                    t.HinhAnh = SanPhamBUS.GetById(n.SanPhamId.Value).HinhAnh;
                    all.Add(t);
                }

                gvNhatKyAll.DataSource = all;
                gvNhatKyAll.DataBind();

                var wins = NguoiThangCuocBUS.GetListNguoiThangCuoc(nguoiDung.Id);
                List<Temp> temp2 = new List<Temp>();
                foreach (var n in wins)
                {
                    Temp t = new Temp();
                    t.Id = n.Id;
                    t.SanPhamId = n.SanPhamId.Value;
                    t.TenSanPham = SanPhamBUS.GetById(n.SanPhamId.Value).TenSanPham;
                    t.GiaSan = SanPhamBUS.GetById(n.SanPhamId.Value).GiaSan.Value;
                    t.GiaHienTai = n.Gia.Value;
                    t.ThoiGian = n.ThoiGian.Value;
                    t.HinhAnh = SanPhamBUS.GetById(n.SanPhamId.Value).HinhAnh;
                    temp2.Add(t);
                }

                gvNhatKyDauGia.DataSource = temp2;
                gvNhatKyDauGia.DataBind();
            }
        }

        protected void gvNhatKyDauGia_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.DataItem == null)
                return;
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label type = (Label)e.Row.FindControl("lbId");
                Button imageButton = (Button)e.Row.FindControl("imgPayment");
                NguoiThangCuoc ntc = NguoiThangCuocBUS.GetById(int.Parse(type.Text));
                if (ntc.ThanhToan == 1)
                {
                    imageButton.Text = "Đã thanh toán";
                    imageButton.Enabled = false;
                    imageButton.Attributes.Add("onclick", "document.body.style.cursor = 'wait';");
                }
            }
        }

        protected void gvNhatKyDauGia_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Payment"))
            {
                int id = int.Parse(e.CommandArgument.ToString());
                NguoiThangCuoc ntc = NguoiThangCuocBUS.GetById(id);
                string redirecturl = "";

                //Mention URL to redirect content to paypal site
                redirecturl += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" + "nguyen_1359476059_biz@gmail.com";

                //First name i assign static based on login details assign this value
                redirecturl += "&first_name=" + NguoiDungBUS.GetById(ntc.NguoiDungId.Value).TaiKhoan;

                //City i assign static based on login user detail you change this value
                redirecturl += "&city=HCM";

                //State i assign static based on login user detail you change this value
                redirecturl += "&state=tamilnadu";

                //Product Name
                redirecturl += "&item_name=" + SanPhamBUS.GetById(ntc.SanPhamId.Value).TenSanPham;

                //Product Amount
                redirecturl += "&amount=" + (int)(ntc.Gia / 22000);

                //Business contact id
                redirecturl += "&business=" + NguoiDungBUS.GetById(ntc.NguoiDungId.Value).Email;

                //Shipping charges if any
                //redirecturl += "&shipping=0";

                //Handling charges if any
                //redirecturl += "&handling=5";

                //Tax amount if any
                //redirecturl += "&tax=5";

                //Add quatity i added one only statically 
                redirecturl += "&quantity=1";

                //Currency code 
                redirecturl += "&currency_code=USD";

                //Success return page url
                redirecturl += "&return=" + "http://localhost:13879/Payment.aspx?Id=" + ntc.Id;

                //Failed return page url
                redirecturl += "&cancel_return=" + "http://localhost:13879/Error.aspx";

                Response.Redirect(redirecturl);
            }
        }
    }
}